package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/3/4 14:52
 * @description
 */
public class Code443 {
    public static void main(String[] args) {
        System.out.println(new Code443().compress(new char[] {'a'}));
    }

    public int compress(char[] chars) {
        char c = chars[0];
        int index = 0, count = 1;
        for (int i = 1; i < chars.length; i++) {
            //如果相等则相加次数,不等就写入
            if (c == chars[i]) {
                count++;
            } else {
                index = getIndex(chars, c, index, count);
                c = chars[i];
                count = 1;
            }
        }

        return getIndex(chars, c, index, count);
    }

    private int getIndex(char[] chars, char c, int index, int count) {
        chars[index++] = c;
        //次数大于1则写入次数
        if (count > 1) {
            char[] cs = (count + "").toCharArray();
            for (char c1 : cs) {
                chars[index++] = c1;
            }
        }
        return index;
    }
}
